/*
 * Created on 2005-9-13
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.wooophone.common.web.action;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Clark
 * 
 *         TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class PageBar {

	private Pager pager;
	private String withoutPageNoUrl;
	
	private String rootPath;

	/**
	 * 当前页号
	 */
	public static final String PCN_PAGE_NO = "_page_no";

	public PageBar(Pager pager, String withoutPageNoUrl) {
		this.pager = pager;
		this.withoutPageNoUrl = withoutPageNoUrl;
	}

	/**
	 * @return Returns the page.
	 */
	public Pager getPage() {
		return pager;
	}

	/**
	 * @param page
	 *            The page to set.
	 */
	public void setPage(Pager pager) {
		this.pager = pager;
	}

	public int getGroupSize() {
		return pager.getGroupSize();
	}

	public int getPageSize() {
		return pager.getPageSize();
	}

	public int getTotalPageCount() {
		return pager.getTotalPageCount();
	}

	public int getTotalGroupCount() {
		return pager.getTotalGroupCount();
	}

	public int getTotalRecordCount() {
		return pager.getRecordCount();
	}

	public int getCurrPageNo() {
		return pager.getCurrPageNo();
	}

	public int getCurrGroupNo() {
		if (getGroupSize() <= 0) {
			return 1;
		} else {
			return (getCurrPageNo() % getGroupSize() > 0) ? getCurrPageNo()
					/ getGroupSize() + 1 : getCurrPageNo() / getGroupSize();
		}
	}

	public int getFirstPageNo() {
		return 1;
	}

	public int getFirstGroupNo() {
		return 1;
	}

	/**
	 * 最后一页
	 * @return
	 */
	public int getLastPageNo() {
		return getTotalPageCount();
	}

	
	public int getLastGroupNo() {
		return getTotalGroupCount();
	}

	/**
	 * 下一页
	 * @return
	 */
	public int getNextPageNo() {
		if (getCurrPageNo() == getTotalPageCount()) {
			return getCurrPageNo();
		} else {
			return getCurrPageNo() + 1;
		}
	}

	public int getNextGroupNo() {
		if (getCurrGroupNo() == getTotalGroupCount()) {
			return getCurrGroupNo();
		} else {
			return getCurrGroupNo() + 1;
		}
	}

	public int getPriorPageNo() {
		if (getCurrPageNo() == 1) {
			return getCurrPageNo();
		} else {
			return getCurrPageNo() - 1;
		}
	}

	public int getPriorGroupNo() {
		if (getCurrGroupNo() == 1) {
			return getCurrGroupNo();
		} else {
			return getCurrGroupNo() - 1;
		}
	}

	/**
	 * 获得指定页URL
	 * @param pageNo
	 * @return
	 */
	public String getPageUrl(int pageNo) {
		StringBuffer pageUrl = new StringBuffer("");
		pageUrl.append(getWithoutPageNoUrl());
		if (getWithoutPageNoUrl().indexOf("?") > 0) {
			pageUrl.append("&");
		} else {
			pageUrl.append("?");
		}
		pageUrl.append(PCN_PAGE_NO).append("=").append(pageNo);
		return pageUrl.toString();
	}
	
	/**
	 * 获得当前页的参数字符串
	 * @return
	 */
	public String getCurrPageParameterString(){
		String url = getPageUrl(getCurrPageNo());
		if(url.indexOf("?")!=-1){
			return url.substring(url.indexOf("?")+1);
		}else{
			return "";
		}
	}
	
	/**
	 * 得到页面URL，页码留空待提供：如:http://www.baidu.com?_page_no=
	 * 
	 * @return URL字符串
	 */
	public String getPageUrlNoPageNumber() {
		StringBuffer pageUrl = new StringBuffer("");
		pageUrl.append(getWithoutPageNoUrl());
		if (getWithoutPageNoUrl().indexOf("?") > 0) {
			pageUrl.append("&");
		} else {
			pageUrl.append("?");
		}
		pageUrl.append(PCN_PAGE_NO).append("=");
		return pageUrl.toString();
	}

	/**
	 * 获得首页URL
	 * @return
	 */
	public String getFirstPageUrl() {
		if (getFirstPageNo() < getCurrPageNo()) {
			return getPageUrl(getFirstPageNo());
		} else {
			return "";
		}
	}

	/**
	 * 获得尾页URL
	 * @return
	 */
	public String getLastPageUrl() {
		if (getLastPageNo() > getCurrPageNo()) {
			return getPageUrl(getLastPageNo());
		} else {
			return "";
		}
	}

	/**
	 * 获得下一页URL
	 * @return
	 */
	public String getNextPageUrl() {
		if (getNextPageNo() > getCurrPageNo()) {
			return getPageUrl(getNextPageNo());
		} else {
			return "";
		}
	}

	/**
	 * 上一页
	 * @return
	 */
	public String getPriorPageUrl() {
		if (getPriorPageNo() < getCurrPageNo()) {
			return getPageUrl(getPriorPageNo());
		} else {
			return "";
		}
	}

	public String getFirstGroupUrl() {
		if (getFirstGroupNo() < getCurrGroupNo()) {
			return getPageUrl(getGroupSize() * getFirstGroupNo());
		} else {
			return "";
		}
	}

	public String getPriorGroupUrl() {
		if (getPriorGroupNo() < getCurrGroupNo()) {
			return getPageUrl(getGroupSize() * getPriorGroupNo());
		} else {
			return "";
		}
	}

	public String getNextGroupUrl() {
		if (getNextGroupNo() > getCurrGroupNo()) {
			return getPageUrl(getGroupSize() * (getNextGroupNo() - 1) + 1);
		} else {
			return "";
		}
	}

	public String getLastGroupUrl() {
		if (getLastGroupNo() > getCurrGroupNo()) {
			return getPageUrl(getGroupSize() * (getLastGroupNo() - 1) + 1);
		} else {
			return "";
		}
	}

	public List<Integer> getPageNoGroupList() {
		List<Integer> list = new ArrayList<Integer>();
		int firstPageNo = (getGroupSize() * (getCurrGroupNo() - 1)) + 1;
		int lastPageNo = (getGroupSize() * getCurrGroupNo() > getTotalPageCount()) ? getTotalPageCount()
				: getGroupSize() * getCurrGroupNo();
		for (int i = firstPageNo; i <= lastPageNo; i++) {
			list.add(i);
		}
		return list;
	}

	public String getWithoutPageNoUrl() {
		return withoutPageNoUrl;
	}

	public void setWithoutPageNoUrl(String withoutPageNoUrl) {
		this.withoutPageNoUrl = withoutPageNoUrl;
	}

	public String getRootPath() {
		return rootPath;
	}

	public void setRootPath(String rootPath) {
		this.rootPath = rootPath;
	}
}
